/*
* Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const { CustomBase } = require("./CustomBase");
const { X2DBeta } = require("../../../engine/graphics/X2DInterface");
const { Scr } = require("../../../engine/XDefine");
const { XTools } = require("../../../engine/XTools");
const { LogManager } = require("../LogManager");

class CustomMessage extends CustomBase {
    constructor(name) {
        super("message", name);
        this.param = [];
    }
    onDrawExtend(dx, dy) {
        let y = dy;
        let ls = this.compose().split("\n");
        for (let l of ls) {
            if (l.trim().length == 0) continue;
            X2DBeta.drawText(l, 12, dx + 4, y + 8, 1, 1, 0, -1, -2, 0xff7bed9f);//e0e0e0
            y += 16;
        }
        return y - dy;
    }
    onDraw(offx = 0, offy = 0, locked = false) {
        super.onDraw(offx, offy, locked);
    }
    onTouch(msg, x, y) {
        if (super.onTouch(msg, x, y)) {
            return true;
        }
        return false;
    }
    compose() {// 把param合成文本
        let txt = "";
        for (let param of this.param) {
            if (param.pkg_type != "原生类型") txt += param.pkg_type + "/";
            txt += param.type;
            if (param.is_array) {
                txt += "[";
                if (param.array_size > 0) {
                    txt += param.array_size;
                }
                txt += "]";
            }
            txt += " " + param.name;
            if (param.is_const) {
                txt += " = " + param.const_value;
            }
            txt += "\n";
        }
        return txt;
    }
    parse(txt) {// 解析一行消息
        const regex = /^([a-zA-Z0-9_/]+)([\[\]0-9]*) +([a-zA-Z-_0-9]+) *(= *(.*))*/;
        let tmpParams = [];
        for (let line of txt.split("\n")) {
            let k = line.match(regex);
            if (k) {
                let tmpParam = {
                    name: k[3],

                    is_const: false,
                    const_value: "",
                    is_array: false,
                    array_size: 0,
                };
                const { EnvManager } = require("../EnvManager");
                let em = EnvManager.getInstance();
                if (k[1].indexOf("/") > 0) {
                    let t = k[1].split("/");
                    if (em.msgKeys.indexOf(t[0]) >= 0 &&
                        em.msgValues[t[0]].indexOf(t[1]) >= 0) {
                        tmpParam.pkg_type = t[0];
                        tmpParam.type = t[1];
                    } else {
                        LogManager.E(`"${line}" 中的类型 ${k[1]} 未定义`);
                        return false;
                    }
                } else {
                    if (em.msgValues["原生类型"].indexOf(k[1]) >= 0) {
                        tmpParam.pkg_type = "原生类型";
                        tmpParam.type = k[1];
                    } else {
                        LogManager.E(`"${line}" 中的类型 ${k[1]} 未定义`);
                        return false;
                    }
                }
                if (k[2].length > 0) {
                    if (k[2][0] == "[") {
                        tmpParam.is_array = true;
                        tmpParam.array_size = 0;
                        if (k[2].length > 2) {
                            tmpParam.array_size = parseInt(k[2].slice(1, -1));
                        }
                    }
                }
                if (k[4]) {
                    tmpParam.is_const = true;
                    tmpParam.const_value = k[4];
                }
                tmpParams.push(tmpParam);
            } else if (line.trim().length > 0) {
                LogManager.E(`"${line}" 不符合规范`)
                return false;
            }
        }
        this.param = tmpParams;
        return true;
    }
    // this.tmpParam = {
    //     pkg_type: k,
    //     type: EnvManager.getInstance().msgValues[k][0],
    //     name: "",
    //     is_const: false,
    //     const_value: "",
    //     is_array: false,
    //     array_size: 0,
    // };
}

module.exports = {
    CustomMessage,
}